****** Extract Experimental Data **********
clear
** set directory to the corresponding raw data folder
cd  "/Users/mustafayahsi/Desktop/The Seeds of Success/z-Tree/raw_data/"

clear
ztree2stata subjects using 151109_1153.xls
gen exp = 1
gen p = 0
save session1, replace

clear
ztree2stata subjects using 151109_1254.xls
gen exp = 2
gen p = 1
save session2, replace

clear
ztree2stata subjects using 151109_1408.xls
gen exp = 3
gen p = 0
save session3, replace

clear
ztree2stata subjects using 151109_1500.xls
gen exp = 4
gen p = 1
save session4, replace

clear
ztree2stata subjects using 151110_1141.xls
gen exp = 5
gen p = 0
save session5, replace

clear
ztree2stata subjects using 151110_1257.xls
gen exp = 6
gen p = 1
save session6, replace


clear
ztree2stata subjects using 151111_1126.xls
gen exp = 7
gen p = 0
save session7, replace

clear
ztree2stata subjects using 151111_1250.xls
gen exp = 8
gen p = 1
save session8, replace

clear
ztree2stata subjects using 151111_1403.xls
gen exp = 9
gen p = 0
save session9, replace

clear
ztree2stata subjects using 151111_1444.xls
gen exp = 10
gen p = 1
save session10, replace

**** Merge Experimental Data******
clear 
use session1
append using session2
append using session3
append using session4 
append using session5 
append using session6
append using session7 
append using session8 
append using session9
append using session10
   
bysort exp Group Period: egen group_avg = mean(contribution)
gen others_avg = ((group_avg*4)-contribution)/3
gen id = 100*exp + Subject


preserve
keep if treatment==1
drop age male living siblings older_siblings trust risk TimeDevamAOK major econ friends member rely TimeDevamBOK
**** Save Contribution Data******
save contributions, replace
restore

preserve 
keep if treatment==2
keep id age male living siblings older_siblings trust risk TimeDevamAOK major econ friends member rely TimeDevamBOK
**** Save Survey Data*****
save survey, replace
restore


***************** Exract Experimental Data*****

clear
ztree2stata contracts using 151109_1254.xls
gen exp = 2
save s2, replace

clear
ztree2stata contracts using 151109_1500.xls
gen exp = 4
save s4, replace

clear
ztree2stata contracts using 151110_1257.xls
gen exp = 6
save s6, replace

clear
ztree2stata contracts using 151111_1250.xls
gen exp = 8
save s8, replace

clear
ztree2stata contracts using 151111_1444.xls
gen exp = 10
save s10, replace

*** Save Base Data****
clear
use s2
append using s4
append using s6
append using s8
append using s10
save base, replace

**** Save Received Data*****
clear
use base
collapse (sum) Points, by(exp Period Receiver)
replace Points = Points * (-1)
gen id = 100*exp + Receiver
drop exp
rename Points received
sort id Period
order id Period
bysort id: g received_t_1=received[_n-1]
save received, replace

**** Save Assigned Data*****
clear
use base
collapse (sum) Points, by(exp Period Sender)
replace Points = Points * (-1)
gen id = 100*exp + Sender
drop exp
rename Points assigned
sort id Period
order id Period
bysort id: g assigned_t_1=assigned[_n-1]
save assigned, replace

*** Generate Punishment Data****
clear
use base
gen id = 100*exp + Sender
gen grp = 100*exp + Group
order id
merge m:1 id using survey
**** Since survey includes N-experiment it will be unmatched
drop if Points==.
drop _merge
merge m:m id Period using received
drop _merge
merge m:m id Period using assigned
drop _merge
bysort exp Group Period Sender: egen others_avg = mean(OthersContribution)
bysort exp Group Period Sender: egen others_min = min(OthersContribution)
gen group_min = 0
replace group_min=1 if others_min >= SendersContribution
gen other_2_avg = (others_avg*3-OthersContribution) / 2
gen more=0
replace more=1 if SendersContribution > OthersContribution
gen notmore = 1-more
replace Points = (-1)*Points
save punish, replace

**************** Prepare Data for Table B.4,B.5***************
clear
use punish
gen punished= 0
replace punished= 1 if Points>0
gen final_period = 0
replace final_period=1 if Period==10
gen younger_siblings=0
replace younger_siblings = siblings - older_siblings
global xlist age male younger_siblings older_siblings trust risk econ friends member rely

**** Table B.4: Social versus Antisocial Punishment
tabulate punished if SendersContribution <= OthersContribution
summarize Points if SendersContribution <= OthersContribution

tabulate punished if SendersContribution > OthersContribution
summarize Points if SendersContribution > OthersContribution
***** Table B.5: Punishment Decisions
eststo clear
eststo: quietly probit punished OthersContribution SendersContribution other_2_avg if Period==1 & OthersContribution <SendersContribution ,nocons cluster(grp)
eststo: quietly probit punished OthersContribution SendersContribution other_2_avg $xlist if Period==1 & OthersContribution <SendersContribution ,nocons cluster(grp)
eststo: quietly probit punished OthersContribution SendersContribution other_2_avg if Period==1 & OthersContribution >=SendersContribution ,nocons cluster(grp)
eststo: quietly probit punished OthersContribution SendersContribution other_2_avg $xlist if Period==1 & OthersContribution >=SendersContribution ,nocons cluster(grp)
eststo: quietly probit punished OthersContribution SendersContribution other_2_avg received_t_1 Period final_period, nocons cluster(grp)
eststo: quietly probit punished OthersContribution SendersContribution other_2_avg received_t_1 Period final_period $xlist, nocons cluster(grp)
esttab, se ar2 star(* .1 ** .05 *** .01)

****************Prepare Data for Table 1,B.1,B.2,B.3***************
clear
use contributions
merge m:1 id using survey
drop _merge
merge m:m id Period using received
drop _merge
merge m:m id Period using assigned
drop _merge
sort id Period
order id Period
bysort id: g others_avg_t_1=others_avg[_n-1]
bysort id: g contribution_t_1=contribution[_n-1]
gen antisocial = 0
replace antisocial = received if contribution >= others_avg
gen social = 0
replace social = received if contribution < others_avg
bysort id: g antisocial_t_1=antisocial[_n-1]
bysort id: g social_t_1=social[_n-1]


gen final_period = 0
replace final_period=1 if Period==10
gen younger_siblings=0
replace younger_siblings = siblings - older_siblings
gen grp = 100*exp + Group
gen better = 0
replace better=1 if contribution >= group_avg
gen rec_ant = received * better
gen ass_ant = assigned * (1-better)
bysort id: g rec_ant_t_1=rec_ant[_n-1]
bysort id: g better_t_1=better[_n-1]

global xlist age male younger_siblings older_siblings trust risk econ friends member rely

******************** Table 1: Mean Contributions and Earnings***************
*1st period avg contribution, N-experiment vs P-experiment
preserve
drop if Period!=1
sort grp id
bysort grp: egen c1=mean(contribution)
mean c1 if p==0
mean c1 if p==1
collapse p c1, by(grp)
ranksum c1, by(p)
restore

*1-10th period avg contribution, N-experiment vs P-experiment
preserve
sort grp id Period
bysort grp Period: egen c2=mean(contribution)
mean c2 if p==0
mean c2 if p==1
collapse p c2, by(grp)
ranksum c2, by(p)
restore

** power analysis on the N-experiment and P-experiment difference on average contributions
preserve
bysort grp: egen c_avg=mean(contribution)
egen cont_n=mean(c_avg) if p==0
egen cont_p=mean(c_avg) if p==1
sort grp
quietly by grp:  gen dup = cond(_N==1,0,_n)
keep if dup==1
drop dup
egen sd_cont_n=sd(c_avg) if p==0
egen sd_cont_p=sd(c_avg) if p==1
bysort treatment: egen c_avg_n=max(cont_n) 
bysort treatment: egen c_avg_p=max(cont_p) 
bysort treatment: egen sd_c_n=max(sd_cont_n) 
bysort treatment: egen sd_c_p=max(sd_cont_p) 
quietly by treatment:  gen dup = cond(_N==1,0,_n)
keep if dup==1
drop dup
summarize c_avg_n c_avg_p sd_c_n sd_c_p
* c_avg_n=6.05 // c_avg_p=10.89 // sd_c_n= 3.542295 // sd_c_p= 5.544993  // sample sizes are 15 // alpha=0.05
* calculate power 
power twomeans 6.05 10.89,  sd1(3.542295)  sd2(5.544993) n1(15) n2(15) alpha(0.05)
restore


*lagged variables
sort id Period
tsset id Period
*******change*******
gen change=.
replace change = 1 if contribution != L.contribution & L.contribution!=.
replace change = 0 if contribution == L.contribution & L.contribution!=.
*increase
gen increase=.
replace increase = 1 if contribution > L.contribution & L.contribution!=.
replace increase = 0 if contribution <= L.contribution & L.contribution!=.
*decrease
gen decrease=.
replace decrease = 1 if contribution < L.contribution & L.contribution!=.
replace decrease = 0 if contribution >= L.contribution & L.contribution!=.

*** Table B.1: Contribution Change by Group Average
preserve
drop if p==0
tab increase if L.group_avg<L.contribution & increase!=0
tab increase if L.group_avg==L.contribution & increase!=0
tab increase if L.group_avg>L.contribution & increase!=0

tab change if L.group_avg<L.contribution & change!=1
tab change if L.group_avg==L.contribution & change!=1
tab change if L.group_avg>L.contribution & change!=1

tab decrease if L.group_avg<L.contribution & decrease!=0
tab decrease if L.group_avg==L.contribution & decrease!=0
tab decrease if L.group_avg>L.contribution & decrease!=0

restore

****Table B.2: Determinants of Contributions Changes (LOGIT)***
eststo clear
eststo:quietly logit change L.received L.others_avg L.contribution Period final_period , nocons cluster(grp)
eststo:quietly logit change L.received L.others_avg L.contribution Period final_period $xlist, nocons cluster(grp)
eststo:quietly logit increase L.received L.others_avg L.contribution Period final_period if  L.contribution < L.others_avg, nocons cluster(grp)
eststo:quietly logit increase L.received L.others_avg L.contribution Period final_period $xlist if  L.contribution < L.others_avg, nocons cluster(grp)
eststo:quietly logit decrease L.received L.others_avg L.contribution Period final_period if  L.contribution >= L.others_avg, nocons cluster(grp)
eststo:quietly logit decrease L.received L.others_avg L.contribution Period final_period $xlist if  L.contribution >= L.others_avg, nocons cluster(grp)
esttab, se ar2 star(* .1 ** .05 *** .01)


**** Table B.3: Determinants of Contributions (OLS)***
eststo clear
eststo:quietly reg contribution L.received L.others_avg  L.contribution Period final_period if change==1,nocons cluster(grp)
eststo:quietly reg contribution L.received L.others_avg  L.contribution Period final_period $xlist if change==1 ,nocons cluster(grp)
eststo:quietly reg contribution L.received L.others_avg  L.contribution Period final_period  if L.contribution < L.others_avg & increase==1 ,nocons cluster(grp)
eststo:quietly reg contribution L.received L.others_avg  L.contribution Period final_period $xlist if L.contribution < L.others_avg & increase==1  ,nocons cluster(grp)
eststo:quietly reg contribution L.received L.others_avg  L.contribution Period final_period  if L.contribution >= L.others_avg  & decrease==1,nocons cluster(grp)
eststo:quietly reg contribution L.received L.others_avg  L.contribution Period final_period $xlist if L.contribution >= L.others_avg & decrease==1 ,nocons cluster(grp)
esttab, se ar2 star(* .1 ** .05 *** .01)


******* Residual Errors OLS
*to be used in agent based simulation

reg contribution L.received L.others_avg  L.contribution Period final_period  if L.contribution < L.others_avg & increase==1 ,nocons cluster(grp)
predict resinc,r
reg contribution L.received L.others_avg  L.contribution Period final_period  if L.contribution >= L.others_avg & decrease==1,nocons cluster(grp)
predict resdec,r

**used residual errors in simulation 
mean resinc if L.contribution < L.others_avg & increase==1, vce(cluster grp)
mean resdec if L.contribution >= L.others_avg & decrease==1, vce(cluster grp)


**** Frequency of 0 (zero) contributions in N-experiment ( Section: 3.1 N-experiment)**********
preserve
drop if Period!=1
drop if p!=0
gen freqc=1 if contribution!=0
replace freqc=0 if contribution==0
tab freqc if p==0 & Period<=5
restore
 
preserve
drop if Period!=10
drop if p!=0
collapse group_avg, by( grp)
tab group_avg
restore
*******

***********Figure 2: Evolution of Average Contributions in the P-experiment*********

graph twoway (line group_avg Period if id == 201, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 209, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 401, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 601, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 609, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 205, lcolor(gs7) lwidth(thick)) (line group_avg Period if id == 805, lcolor(gs3) lwidth(thick)) , graphregion(color(white)) scheme(s1mono) legend(off) ytitle("Contribution") ylabel(0(5)20) yscale(range(0 20)) 
 
graph twoway (line group_avg Period if id == 405, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 409, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 605, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 801, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 809, lcolor(gs1) lwidth(thick)) (line group_avg Period if id == 1001, lcolor(gs7) lwidth(thick)) (line group_avg Period if id == 1005, lcolor(gs3) lwidth(thick)) (line group_avg Period if id == 1009, lcolor(gs3) lwidth(thick)) , graphregion(color(white)) scheme(s1mono) legend(off) ytitle("Contribution") ylabel(0(5)20) yscale(range(0 20)) 
  
*********Figure 1: Timeline of Average Contribution by Treatment********
sort grp id Period
preserve
bysort grp Period: egen c=mean(contribution) 
collapse c , by(p Period)
gen p2="N-experiment"
replace p2="P-experiment" if p==1
twoway (line c Period),  graphregion(color(white)) scheme(s1mono) legend(off) ytitle(" (Mean) Contribution") ylabel(0(5)20) yscale(range(0 20)) by(p2, note(""))
restore

**************      PART1: RUN THIS SECTION BEFORE RUNNING R CODE   *********************
  
******construct true average contribution data from experiment for simulation*****
preserve
drop if p==0
keep id Period session Group contribution grp group_avg
collapse group_avg, by(Period grp)
export excel using "/Users/mustafayahsi/Desktop/The Seeds of Success/Stata/Stata Produced Data/oursaverage.xlsx", replace
restore

** retrieve 1st period actual contribution for simulation
preserve
keep if Period==1
keep if p==1
keep id Period session Subject Group contribution group_avg
export excel using "/Users/mustafayahsi/Desktop/The Seeds of Success/Stata/Stata Produced Data/firstperiod.xlsx", firstrow(variables) replace
restore	 
**** generate herrmann data for R programming *******
clear
import delimited "/Users/mustafayahsi/Desktop/The Seeds of Success/Others/HerrmannThoeniGaechterDATA.csv", varnames(4) rowrange(1) clear
preserve
keep if period==1 | period==10
keep if secseq==1
rename groupid grp
rename period Period
bysort grp Period: egen group_avg = mean(senderscontribution)
collapse group_avg   , by(Period grp sessionid mgroupid p secseq city)
sort city grp Period
export delimited using "/Users/mustafayahsi/Desktop/The Seeds of Success/Stata/Stata Produced Data/herrmannallcitiesp.csv", replace
*********************************************************************************

**************      PART2: RUN THIS SECTION ONLY AFTER RUNNING R CODE1   *********************
** generate herrmann data for 6 cities resembling to Istanbul
clear
import delimited "/Users/mustafayahsi/Desktop/The Seeds of Success/R-Studio/R Produced Data/6cities.csv", rowrange(1) clear
xtset city2
xtreg group_avg2 group_avg1, fe
predict fitted, xb
predict se, stdp
gen upper_ci=1.96*se+fitted
gen lower_ci=-1.96*se+fitted
export delimited using "/Users/mustafayahsi/Desktop/The Seeds of Success/Stata/Stata Produced Data/herrmann-fe6cities.csv", replace


** Regression: 10th period contribution on 1st period contribution, herrmann data, only Istanbul
clear
import delimited "/Users/mustafayahsi/Desktop/The Seeds of Success/R-Studio/R Produced Data/allcities.csv", rowrange(1) clear
keep if city=="Istanbul"
reg group_avg2 group_avg1
****************************************************************************
